package com.icreate.storm.methods.indicators;

import com.icreate.entity.indicator.IndicatorID;
import com.icreate.entity.indicator.S1_MMEfield;
import com.icreate.storm.methods.common.DateMethod;
import org.apache.storm.tuple.Values;

/**
 * Created by yc on 2017/5/12.
 */
public class S1_MMEIndicator extends BaseIndicator {
    public static final String typeName = "mltes1mme";
    public static final String[] fields = {"type", "cellid", "imsi", "imei", "IndicatorID",
            "mltes1mme.attach_request", "mltes1mme.attach_success",
            "mltes1mme.service_request", "mltes1mme.service_request_success",
            "mltes1mme.contextdef_activate_request", "mltes1mme.contextdef_activate_success",
            "mltes1mme.x2_inter_request", "mltes1mme.x2_inter_success",
            "mltes1mme.tau_request", "mltes1mme.tau_success",
            "mltes1mme.s1_inter_request", "mltes1mme.s1_inter_success",
            "mltes1mme.pdn_request", "mltes1mme.pdn_request_success",
    };
    public static final String[] baseFields = {
            "mltes1mme.attach_request", "mltes1mme.attach_success",
            "mltes1mme.service_request", "mltes1mme.service_request_success",
            "mltes1mme.contextdef_activate_request", "mltes1mme.contextdef_activate_success",
            "mltes1mme.x2_inter_request", "mltes1mme.x2_inter_success",
            "mltes1mme.tau_request", "mltes1mme.tau_success",
            "mltes1mme.s1_inter_request", "mltes1mme.s1_inter_success",
            "mltes1mme.pdn_request", "mltes1mme.pdn_request_success",
    };

    @Override
    public String getTypeName() {
        return typeName;
    }

    @Override
    public String[] getBaseFields() {
        return baseFields;
    }

    public String[] getFields() {
        return fields;
    }

    public Values getValues(Object s) {
        S1_MMEfield field = (S1_MMEfield) s;

        long status = getValue(field.getStatus());
        boolean valid = true;
        if (field.getStatus().equals(""))
            valid = false;
        long procedureType = getValue(field.getProcedureType());
        long cause = getValue(field.getCause());


        Values values = new Values("mltes1mme", field.getCellid(), field.getImsi(), field.getImei(), field);
        for (long i = 1; i <= 7; i++) {
            if (!valid) {
                values.add(0);
                values.add(0);
                // continue;
            } else {
                if (i == 1) {
                    values.add(getRequest1(i, procedureType));
                    values.add((getSuccess1(i, procedureType, status, cause)));
                } else if (i == 2 || i == 5) {
                    values.add(getRequest1(i, procedureType));
                    values.add(getSuccess2(i, procedureType, status, cause));
                } else if (i == 7) {
                    values.add(getRequest1(i, procedureType));
                    values.add(getSuccess(i, procedureType, status));
                } else if (i == 3) {
                    values.add((getRequest1(102, procedureType)));
                    values.add((getSuccess(102, procedureType, status)));
                } else {
                    values.add((getRequest1(i * 10, procedureType)));
                    values.add((getSuccess(i * 10, procedureType, status)));
                }
            }
        }
        return values;
    }

    private long getRequest1(long type, long procedureType) {
        if (procedureType == type)
            return 1;
        else
            return 0;
    }

    private long getSuccess(long type, long procedureType, long status) {
        if (procedureType == type && status == 0)
            return 1;
        else
            return 0;
    }

    private long getSuccess1(long type, long procedureType, long status, long cause) {
        long[] count = {2, 3, 6, 7, 8, 9, 11, 12, 13, 14, 15, 25, 35};
        if ((procedureType == type && status == 0) || (procedureType == type && status == 1 && judge_cause(cause, count)))
            return 1;
        else
            return 0;
    }

    private long getSuccess2(long type, long procedureType, long status, long cause) {
        long[] count = {2, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 25, 35};
        if ((procedureType == type && status == 0) || (procedureType == type && status == 1 && judge_cause(cause, count)))
            return 1;
        else
            return 0;
    }

    private boolean judge_cause(long cause, long[] count) {
        boolean judg = false;
        for (int i = 0; i < count.length; i++) {
            if (cause == count[i]) judg = true;
            else ;
        }
        return judg;
    }

}
